import Vue from 'vue'
import VueRouter from 'vue-router'
import { resError } from '@/common/js/global'
import { getItem, getToken } from '@/common/utils/cookieUtil' // 验权
import store from '../store'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

/**
 * 静态路由
 */
export const constantRouterMap = [
    {
        path: '/dashboard',
        name: "dashboard",
        meta: {
            icon: "el-icon-s-home",
            title: "桌面",
        },
        hidden: false,
        sort: null,
        component: () => import('@/views/dashboard'),
    },
    {
        path: '/login',
        meta: {
            icon: null,
            title: "登录",
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/login'),
    },
    // {
    //     path: '*',
    //     meta: {
    //         icon: null,
    //         title: "404",
    //     },
    //     hidden: true,
    //     sort: null,
    //     component: () => import('@/404'),
    // },
    {
        path: '/user',
        meta: {
            icon: "el-icon-user-solid",
            title: "用户管理",
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "list",
                meta: {
                    icon: null,
                    title: "用户列表",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/user/user-list'),
            },

        ]
    },
    {
        path: '/permission-menu',
        name: "permission-menu",
        meta: {
            icon: "el-icon-star-on",
            title: "权限和菜单",
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'user',
                name: "user",
                meta: {
                    icon: null,
                    title: "用户权限",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/user-permission'),
            },
            {
                path: 'role',
                name: "role",
                meta: {
                    icon: null,
                    title: "角色权限",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/role-permission'),
            },
            {
                path: 'add-permission',
                name: "add-permission",
                meta: {
                    icon: null,
                    title: "新增权限",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/add-permission'),
            },
            {
                path: 'menu',
                name: "menu",
                meta: {
                    icon: null,
                    title: "菜单列表",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/menu-list'),
            },
            {
                path: 'add-menu',
                name: "add-menu",
                meta: {
                    icon: null,
                    title: "新增菜单",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/add-menu'),
            },
        ]
    },
    {
        path: '/system',
        name: "system",
        meta: {
            icon: "el-icon-s-tools",
            title: "系统管理",
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'setting',
                name: "setting",
                meta: {
                    icon: null,
                    title: "系统设置",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/system/setting'),
            },
            {
                path: 'login-log',
                name: "login-log",
                meta: {
                    icon: null,
                    title: "登录日志",
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/system/login-log'),
            },

        ]
    },

]
/**
 * 动态路由
 */
export const asyncRouterMap = [

]


const router = new VueRouter({
    mode: 'history',
    routes: constantRouterMap,
    // 控制路由跳转滚动条的位置为最顶部
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部，y代表距离顶部的距离
        return { y: 0 }
    },
})

const whiteList = ['/login'] // 不重定向白名单

router.beforeEach((to, from, next) => {
    // if (getItem("token")) {
    //     if (to.path === '/login') {
    //         next({ path: '/dashboard' })
    //     } else {
    //         if (store.state.user.role.length === 0) {
    //             // 拉取用户信息
    //             getUserInfo(next)
    //         } else {
    //             next()
    //         }
    //     }
    // } else {
    //     if (whiteList.indexOf(to.path) !== -1) {
    //         next()
    //     } else {
    //         next('/login')
    //     }
    // }
    next()

    // 保存历史记录
    if (from.path != '/') {
        store.commit("saveNavHistory", to)
    }
})

async function getUserInfo(next) {
    // 获取用户信息
    let res = await store.dispatch('getUserInfo', {
        id: null
    })
    if (res.success) {
        let menus = null, username = null
        if (identify == 1) {
            menus = res.data.adminMenuList;
            username = res.data.userDto.username;
        }
        else if (identify == 2) {
            menus = res.data.adminMenuList;
            username = res.data.doctorDto.username;
        }
        else {
            menus = res.data.adminMenuList;
            username = res.data.adminDto.username;
        }
        // 生成可访问的路由表
        store.dispatch('GenerateRoutes', { menus, username }).then(() => {
            // 动态添加可访问路由表
            router.addRoutes(store.state.menu.addRouters);
            next()
        })
    }
    else {
        // 退出登录
        // let res = await store.dispatch('logout')
        // if (res.success) {
        //     Message.error(err || 'Verification failed, please login again')
        //     next({ path: '/' })
        // }
        resError(res)
    }

}


export default router